package com.mazaiting.redeye.controller;

import com.mazaiting.redeye.component.RedisTokenManager;
import com.mazaiting.redeye.config.GlobalConfig;
import com.mazaiting.redeye.domain.*;
import com.mazaiting.redeye.service.UserService;
import com.mazaiting.redeye.utils.confuse.OpensslUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/***
 *
 *
 *                                                    __----~~~~~~~~~~~------___
 *                                   .  .   ~~//====......          __--~ ~~
 *                   -.            \_|//     |||\\  ~~~~~~::::... /~
 *                ___-==_       _-~o~  \/    |||  \\            _/~~-
 *        __---~~~.==~||\=_    -_--~/_-~|-   |\\   \\        _/~
 *    _-~~     .=~    |  \\-_    '-~7  /-   /  ||    \      /
 *  .~       .~       |   \\ -_    /  /-   /   ||      \   /
 * /  ____  /         |     \\ ~-_/  /|- _/   .||       \ /
 * |~~    ~~|--~~~~--_ \     ~==-/   | \~--===~~        .\
 *          '         ~-|      /|    |-~\~~       __--~~
 *                      |-~~-_/ |    |   ~\_   _-~            /\
 *                           /  \     \__   \/~                \__
 *                       _--~ _/ | .-~~____--~-/                  ~~==.
 *                      ((->/~   '.|||' -_|    ~~-/ ,              . _||
 *                                 -_     ~\      ~~---l__i__i__i--~~_/
 *                                 _-~-__   ~)  \--______________--~~
 *                               //.-~~~-~_--~- |-------~~~~~~~~
 *                                      //.-~~~--\
 *                               神兽保佑
 *                              代码无BUG!
 * @author mazaiting
 * @date 2019-06-19
 * @decription 用户控制器
 */
@RestController
@RequestMapping(GlobalConfig.USER)
public class UserController {

    /**
     * 日志工具
     */
    private Logger mLog = LoggerFactory.getLogger(UserController.class);
    /**
     * 用户服务
     */
    private final UserService mUserService;
    /**
     * Redis 服务
     */
    private final RedisTokenManager mRedisTokenManager;

    @Autowired
    public UserController(UserService service, RedisTokenManager mRedisService) {
        this.mUserService = service;
        this.mRedisTokenManager = mRedisService;
    }

    /**
     * 获取加密公钥
     * @return 公钥
     */
    @GetMapping(GlobalConfig.USER_PUBLIC_KEY)
    public Response getPublicKey() {
        Response response = new Response();
        // 获取公钥
        String publicKey = OpensslUtil.getPublicKey();
        // 检测公钥是否为空或换行
        if (!OpensslUtil.isValid(publicKey)) {
            // 获取 Pkcs8 编码私钥内容
            String privateKeyPkcs8 = OpensslUtil.getPrivateKey();
            // 检测 Pkcs8 编码私钥是否为空
            if (!OpensslUtil.isValid(privateKeyPkcs8)) {
                // 为空, 获取私钥
                String privateKey = OpensslUtil.catPrivateKey();
                // 检测私钥是否为空
                if (!OpensslUtil.isValid(privateKey)) {
                    // 私钥为空, 生成私钥
                    OpensslUtil.generatePrivateKey();
                }
                // 私钥不为空
                OpensslUtil.pkcs8Encoder();
            }
            // 不为空, 则生成公钥
            OpensslUtil.generatePublicKey();
            // 获取公钥
            publicKey = OpensslUtil.getPublicKey();
        }

        response.setResponse(ResponseCode.SUCCESS, publicKey);
        return response;
    }

    /**
     * 登录
     * @param user 用户信息
     * @return 响应信息
     */
    @PostMapping(GlobalConfig.USER_LOGIN)
    public Response<Request<UserBean>> login(@RequestBody UserBean user, HttpServletRequest request) {
        mLog.debug("登录内容: " + user.toString());
        Response<Request<UserBean>> response = mUserService.login(user);
        if (ResponseCode.SUCCESS == response.getRetCode()) {
            // 获取 SessionID
            String sessionId = request.getSession().getId();
            mLog.debug("login_SessionId: " + sessionId);
            Request<UserBean> userBeanRequest = new Request<>(user);
            String token = mRedisTokenManager.getToken(sessionId, userBeanRequest);
            userBeanRequest.setToken(token);
            userBeanRequest.setData(null);
            response.setData(userBeanRequest);
        }
        return response;
    }

    /**
     * 注册
     * @param user 用户信息
     * @return 响应信息
     */
    @PostMapping(GlobalConfig.USER_REGISTER)
    public Response<Object> register(@RequestBody UserBean user) {
        mLog.debug("注册内容: " + user.toString());

        return mUserService.register(user);
    }

    /**
     * 获取用户信息
     * @param userBeanRequest 用户信息
     * @return 用户信息
     */
    @PostMapping(GlobalConfig.USER_INFORMATION)
    public Response<UserBean> information(@RequestBody Request<UserBean> userBeanRequest) {
        mLog.debug("用户信息: " + userBeanRequest.toJson());
        return mUserService.information(userBeanRequest);
    }

    /**
     * 更新用户信息
     * @param request 用户信息
     * @return 是否成功
     */
    @PostMapping(GlobalConfig.USER_UPDATE_USER)
    public Response updateUser(@RequestBody Request<UserBean> request) {
        mLog.debug("新用户信息: " + request.toJson());
        return mUserService.updateUser(request.getData());
    }

    /**
     * 更新用户手机号
     * @param request 更新用户手机号
     * @return 是否成功
     */
    @PostMapping(GlobalConfig.USER_UPDATE_PHONE_NUMBER)
    public Response updatePhone(@RequestBody Request<VerifyCodeBean<UserBean>> request) {
        mLog.debug("手机号信息: " + request.toJson());
        return mUserService.updatePhoneNumber(request.getData());
    }
}
